package com.foreknow.service.impl;

import com.foreknow.dao.RuleMapper;
import com.foreknow.entity.Rule;
import com.foreknow.entity.User;
import com.foreknow.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class RuleServiceImpl implements RuleService {

    @Autowired
    private RuleMapper ruleMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int deleteByRulenameByDeptIDByLogic(Rule record) {
        return ruleMapper.deleteByRulenameByDeptIDByLogic(record);
    }

    @Override
    public int deleteByRulenameByDeptIDByLogicBatch(List<Rule> rules) {
        return ruleMapper.deleteByRulenameByDeptIDByLogicBatch(rules);
    }

    @Override
    public int deleteByRulenameByLogic(String rulename) {
        return ruleMapper.deleteByRulenameByLogic(rulename);
    }

    @Override
    public int insert(List<User> userList, String rulename){

        //集合
        List<Rule> rules = new ArrayList<>();
        for (User user : userList) {
            Rule ruleitem = new Rule();
            Integer deptid = user.getDeptid();
            Integer userid = user.getId();
            List<String> weekdetail = user.getWeekdetail();
            StringBuilder week=new StringBuilder();
            for (String s : weekdetail) {
                week.append(s);
            }
            ruleitem.setRulename(rulename);
            ruleitem.setDeptid(deptid);
            ruleitem.setUserid(userid);
            ruleitem.setWeek(week.toString());
            //添加
            rules.add(ruleitem);
        }
        return  ruleMapper.insert(rules);
    }

    @Override
    public Rule selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public List<Rule> selectByRulenameDeptId(Rule record) {
        List<Rule> rules = ruleMapper.selectByRulenameDeptId(record);
        //分解字符串
        if (rules.size()>0){
            for (int index = 0; index < rules.size(); index++) {
                Rule item = rules.get(index);
                List<Map<String, String>> weekdetail = new ArrayList<Map<String, String>>();
                String week = item.getWeek();
                //字符串分解
                String[] split = week.split("");
                //int dayindex=1;
                for (int i = 0; i <split.length/2 ; i++) {

                    int temp=i*2;
                    String s1 = split[temp];
                    String s2 = split[temp+1];
                    Map<String,String> map=new HashMap<String, String>();
                    map.put("morning",s1);
                    map.put("afternoon",s2);
                    //将上下午排班信息插入List集合
                    weekdetail.add(map);

                }
                //将排班明细插入rule项目
                item.setWeekdetail(weekdetail);
                //将rule赋值到返回集合中
                rules.set(index,item);
            }

        }

        return rules;
    }

    @Override
    public List<Rule> selectAllRulesByCondition(Rule record) {
        return ruleMapper.selectAllRulesByCondition(record);
    }

    @Override
    public int updateByPrimaryKeySelective(Rule record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Rule record) {
        return 0;
    }
}
